/* eslint-disable no-param-reassign */
export default {
  methods: {
    valid(key) {
      this.$refs.form && this.$refs.form.validateField(key);
      // this.form = defaultForm()
    },
    selectorDisableFn({ status }) {
      return status !== 0;
    },
    vJump({ type = "push", name, params, query, path }) {
      if (path && name)
        return console.log("error: path can not exist with name");
      if (path && params)
        return console.log("error: path can not exist with params");

      this.$router[type]({
        name,
        path,
        params,
        query
      });
      return undefined;
    },
    vBack() {
      this.$router.back();
    },
    confirmBox({
      onConfirm,
      title = "提示",
      message = "是否确定删除对应的记录？",
      confirmButtonText = "确定",
      cancelButtonText = "取消",
      loadingText = "执行中...",
      type = "warning",
      iconClass = "",
      closeOnClickModal = false,
      showClose = false,
      closeOnPressEscape = false
    }) {
      return new Promise(resolve => {
        this.$confirm(message, title, {
          closeOnClickModal,
          closeOnPressEscape,
          showClose,
          confirmButtonText,
          cancelButtonText,
          type,
          iconClass,
          beforeClose: async (action, instance, done) => {
            if (action === "confirm") {
              instance.confirmButtonLoading = true;
              instance.confirmButtonText = loadingText;
              if (onConfirm) {
                const ret = await onConfirm();
                setTimeout(() => {
                  instance.confirmButtonText = confirmButtonText;
                  instance.confirmButtonLoading = false;
                }, 300);
                if (!ret) return;
              }
              done();
            } else {
              done();
            }
          }
        })
          .then(() => {
            resolve();
          })
          .catch(() => {});
      });
    },
    handleMessage(message, type) {
      if (!message) return;
      this.$message({
        type,
        message
      });
    },
    handleSuccess(message) {
      this.handleMessage(message, "success");
    },
    handleError(message) {
      this.handleMessage(message, "error");
    },
    handleWarning(message) {
      this.handleMessage(message, "warning");
    },
    imgs2List(str) {
      if (!str) return [];
      return str.split(";").filter(i => i);
    },
    logout() {
      this.$store.dispatch("user/logout");

      this.$router.push("/login");
      this.$store.dispatch("tagsView/retsetState");
    },
    onPrint(type, id) {
      let url = "";
      switch (type) {
        case "workOrder":
          url = "/workOrder.html";
          break;
        case "order":
          url = "/order.html";
          break;
        case "afterOrder":
          url = "/afterOrder.html";
          break;
        default:
          break;
      }
      if (!id || !this.token || !url) return;
      sessionStorage.setItem("pr", JSON.stringify(this.token));
      const params = {
        id
      };
      window.open(`${url}?${Util.encodeToURIString(params)}`);
    },
    hasPermission(roles) {
      return Util.hasPermission(roles);
    }
  }
};
